phe<-read.csv('GSE25066_sample.csv',quote='',header=T,row.names=1,check.names=F)
phe$age<-ifelse(phe$age>60,1,0)
phe<-phe[phe$stage!='Inflammatory',]
phe$stage<-ifelse(phe$stage=='I'|phe$stage=='IIA'|phe$stage=='IIB',0,1)
phe<-phe[phe$grade!='4=Indeterminate',]
phe$grade<-ifelse(phe$grade==3,1,0)
phe<-phe[,c(2,5:7,3,4)]
names(phe)<-c('Risk','age','stage','grade','event','time')
library(survival)
library(survminer)
mySurv=with(phe,Surv(time, event))
m_rs=coxph(mySurv~phe$Risk,data = phe)
m_stage=coxph(mySurv~phe$stage,data = phe)
m_age=coxph(mySurv~phe$age,data = phe)
m_grade=coxph(mySurv~phe$grade,data = phe)
m=coxph(mySurv~phe$Risk+phe$age+phe$grade+phe$stage,data = phe)
mul_hr<-exp(coef(m))
mul_beta <- coef(m)
mul_se <- sqrt(diag(vcov(m)))
mul_p <- 1 - pchisq((mul_beta/mul_se)^2, 1)
mul_HRCILL = exp(mul_beta - qnorm(.95, 0, 1) * mul_se)
mul_HRCIUL = exp(mul_beta + qnorm(.95, 0, 1) * mul_se)
mul<-as.data.frame(cbind(coef=mul_beta,HR=mul_hr,p=mul_p,CIL=mul_HRCILL,CIU=mul_HRCIUL))
rownames(mul)<-c('Risk','age','grade','stage')
rs<-c(coef(m_rs),exp(coef(m_rs)),1-pchisq((coef(m_rs)/sqrt(diag(vcov(m_rs))))^2, 1),exp(coef(m_rs) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_rs)))),exp(coef(m_rs) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_rs)))))
stage<-c(coef(m_stage),exp(coef(m_stage)),1-pchisq((coef(m_stage)/sqrt(diag(vcov(m_stage))))^2, 1),exp(coef(m_stage) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_stage)))),exp(coef(m_stage) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_stage)))))
age<-c(coef(m_age),exp(coef(m_age)),1-pchisq((coef(m_age)/sqrt(diag(vcov(m_age))))^2, 1),exp(coef(m_age) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_age)))),exp(coef(m_age) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_age)))))
grade<-c(coef(m_grade),exp(coef(m_grade)),1-pchisq((coef(m_grade)/sqrt(diag(vcov(m_grade))))^2, 1),exp(coef(m_grade) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_grade)))),exp(coef(m_grade) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_grade)))))

uni<-rbind(rs,age,grade,stage)
colnames(uni)<-c('coef','HR','P','CIL','CIU')
uni<-as.data.frame(uni)
rownames(uni)<-rownames(mul)
write.csv(uni,'univariate_cox.csv',quote=F)
write.csv(mul,'multivariate_cox.csv',quote=F)
library(ggplot2)
uni$name<-rownames(uni)
mul$name<-rownames(mul)
library(forestplot)
options(forestplot_new_page = FALSE)
clrs <- fpColors(box="blue",line="brown", summary="royalblue") 
hr1=sprintf("%.3f",uni[,"HR"])
hrLow1=sprintf("%.3f",uni[,"CIL"])
hrHigh1=sprintf("%.3f",uni[,"CIU"])
pVal1=uni$P
pVal1=ifelse(pVal1<0.001, "<0.001", sprintf("%.3f", pVal1))
tabletext1 <- 
    list(c('Risk factors', rownames(uni)),
         append("pvalue", pVal1),
         append("Hazard ratio",paste0(hr1,"(",hrLow1,"-",hrHigh1,")")) ) 
HR1<-uni[,c(2,4,5)]
pdf("univariate_cox.pdf",width = 7, height = 4,onefile=F)
forestplot(tabletext1, 
           rbind(rep(NA, 3), HR1),
           col=clrs,
           graphwidth=unit(50, "mm"),
           xlog=T,
           lwd.ci=2,
           boxsize=0.4,
           xlab="Hazard ratio")
dev.off()

hr2=sprintf("%.3f",mul[,"HR"])
hrLow2=sprintf("%.3f",mul[,"CIL"])
hrHigh2=sprintf("%.3f",mul[,"CIU"])
pVal2=mul$p
pVal2=ifelse(pVal2<0.001, "<0.001", sprintf("%.3f", pVal2))
tabletext2 <- 
    list(c('Risk factors', rownames(mul)),
         append("pvalue", pVal2),
         append("Hazard ratio",paste0(hr2,"(",hrLow2,"-",hrHigh2,")")) ) 
HR2<-mul[,c(2,4,5)]
pdf("multivariate_cox.pdf",width = 7, height = 4,onefile=F)
forestplot(tabletext2, 
           rbind(rep(NA, 3), HR2),
           col=clrs,
           graphwidth=unit(50, "mm"),
           xlog=T,
           lwd.ci=2,
           boxsize=0.4,
           xlab="Hazard ratio")
dev.off()

phe<-read.csv('GSE86166_sample.csv',quote='',header=T,row.names=1,check.names=F)
phe$risk<-ifelse(phe$rs>median(phe$rs),1,0)
phe$stage<-ifelse(phe$stage=='1'|phe$stage=='1A'|phe$stage=='2'|phe$stage=='2A'|phe$stage=='2B',0,1)
phe$grade<-ifelse(phe$grade==1|phe$grade==2,0,1)
phe$risk<-ifelse(phe$rs>median(phe$rs),1,0)
phe<-phe[,c(28,7:8,5:6)]
names(phe)<-c('Risk','grade','stage','event','time')
library(survival)
library(survminer)
mySurv=with(phe,Surv(time, event))
m_rs=coxph(mySurv~phe$Risk,data = phe)
m_stage=coxph(mySurv~phe$stage,data = phe)
m_grade=coxph(mySurv~phe$grade,data = phe)
m=coxph(mySurv~phe$Risk+phe$grade+phe$stage,data = phe)
mul_hr<-exp(coef(m))
mul_beta <- coef(m)
mul_se <- sqrt(diag(vcov(m)))
mul_p <- 1 - pchisq((mul_beta/mul_se)^2, 1)
mul_HRCILL = exp(mul_beta - qnorm(.95, 0, 1) * mul_se)
mul_HRCIUL = exp(mul_beta + qnorm(.95, 0, 1) * mul_se)
mul<-as.data.frame(cbind(coef=mul_beta,HR=mul_hr,p=mul_p,CIL=mul_HRCILL,CIU=mul_HRCIUL))
rownames(mul)<-c('Risk','grade','stage')
rs<-c(coef(m_rs),exp(coef(m_rs)),1-pchisq((coef(m_rs)/sqrt(diag(vcov(m_rs))))^2, 1),exp(coef(m_rs) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_rs)))),exp(coef(m_rs) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_rs)))))
stage<-c(coef(m_stage),exp(coef(m_stage)),1-pchisq((coef(m_stage)/sqrt(diag(vcov(m_stage))))^2, 1),exp(coef(m_stage) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_stage)))),exp(coef(m_stage) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_stage)))))
grade<-c(coef(m_grade),exp(coef(m_grade)),1-pchisq((coef(m_grade)/sqrt(diag(vcov(m_grade))))^2, 1),exp(coef(m_grade) - qnorm(.95, 0, 1) *sqrt(diag(vcov(m_grade)))),exp(coef(m_grade) + qnorm(.95, 0, 1) * sqrt(diag(vcov(m_grade)))))

uni<-rbind(rs,grade,stage)
colnames(uni)<-c('coef','HR','P','CIL','CIU')
uni<-as.data.frame(uni)
rownames(uni)<-rownames(mul)
write.csv(uni,'univariate_cox.csv',quote=F)
write.csv(mul,'multivariate_cox.csv',quote=F)
library(ggplot2)
uni$name<-rownames(uni)
mul$name<-rownames(mul)
library(forestplot)
options(forestplot_new_page = FALSE)
clrs <- fpColors(box="blue",line="brown", summary="royalblue") 
hr1=sprintf("%.3f",uni[,"HR"])
hrLow1=sprintf("%.3f",uni[,"CIL"])
hrHigh1=sprintf("%.3f",uni[,"CIU"])
pVal1=uni$P
pVal1=ifelse(pVal1<0.001, "<0.001", sprintf("%.3f", pVal1))
tabletext1 <- 
    list(c('Risk factors', rownames(uni)),
         append("pvalue", pVal1),
         append("Hazard ratio",paste0(hr1,"(",hrLow1,"-",hrHigh1,")")) ) 
HR1<-uni[,c(2,4,5)]
pdf("univariate_cox.pdf",width = 7, height = 4,onefile=F)
forestplot(tabletext1, 
           rbind(rep(NA, 3), HR1),
           col=clrs,
           graphwidth=unit(50, "mm"),
           xlog=T,
           lwd.ci=2,
           boxsize=0.4,
           xlab="Hazard ratio")
dev.off()

hr2=sprintf("%.3f",mul[,"HR"])
hrLow2=sprintf("%.3f",mul[,"CIL"])
hrHigh2=sprintf("%.3f",mul[,"CIU"])
pVal2=mul$p
pVal2=ifelse(pVal2<0.001, "<0.001", sprintf("%.3f", pVal2))
tabletext2 <- 
    list(c('Risk factors', rownames(mul)),
         append("pvalue", pVal2),
         append("Hazard ratio",paste0(hr2,"(",hrLow2,"-",hrHigh2,")")) ) 
HR2<-mul[,c(2,4,5)]
pdf("multivariate_cox.pdf",width = 7, height = 4,onefile=F)
forestplot(tabletext2, 
           rbind(rep(NA, 3), HR2),
           col=clrs,
           graphwidth=unit(50, "mm"),
           xlog=T,
           lwd.ci=2,
           boxsize=0.4,
           xlab="Hazard ratio")
dev.off()